// 可能是因为用string做了map的key, 所以拓扑序列和AC序列不同
#include<bits/stdc++.h>
using namespace std;
map<string, vector<string>> adjlist;
unordered_map<string, int> indeg;
void solve() {
  int n,m;cin>>n;
  string A, B;
  for(int i=0; i<n; ++i) {
    cin>>A;
    adjlist[A] = vector<string>(0);
    indeg[A] = 0;
  }
  cin>>m;
  for(int i=0; i<m; ++i) {
    cin>>A>>B;
    adjlist[A].push_back(B);
  }

  for(auto [s, v]:adjlist) {
    for(auto el:v) {
      indeg[el]++;
    }
  }
  queue<string> q;
  vector<string> topo;
  for(auto [s,i]:indeg) {
    if(i==0) {
      q.push(s);
    }
  }
  while(!q.empty()) {
    auto u = q.front();q.pop();
    topo.push_back(u);
    for(auto el:adjlist[u]) {
      if(--indeg[el] == 0) {
        q.push(el);
      }
    }
  }
  for(auto el:topo) {
    cout<<' '<<el;
  }
  return;
}
int main() {
  int t = 1;
  do {
    cout << "Case #"<<t<<": Dilbert should drink beverages in this order:";
    solve();
    cout<<".\n\n";
    adjlist.clear();indeg.clear();
  }while(cin.get(),cin.get()!=EOF);
  return 0;
}
